﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Store
{
    public partial class Form : FormPage
    {
        BLL.Store bll = new BLL.Store();
        Model.Store model = new Model.Store();
        //
        BLL.PAUser bllUser = new BLL.PAUser();
        Model.PAUser modelUser = new Model.PAUser();
        //
        BLL.Pic bllPic = new BLL.Pic();
        Model.Pic modelPic = new Model.Pic();
        protected void Page_Load(object sender, EventArgs e) { }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel()
        {
            model = bll.GetModel(Master.id); 
            modelUser = bllUser.GetModel(model.UserID);
            if (DataTypePlus.requestInt(model.PicID) > 0) modelPic = bllPic.GetModel(DataTypePlus.requestInt(model.PicID));
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            for (int i = 0; i <= 6; i++) {
                rbl_BuyDay.Items[i].Text += "" + bll.GetRecordCount("BuyDay=" + i);
            }
            if (Security.GetCurrentUserType() != EnumHelper.UserType.Administrator)
            {
                group_IsLock.Visible = false;
                group_StateCheck.Visible = false;
            }
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            tb_Password_helper.Visible = true;

            tb_Name.Text = model.Name;
            tb_ShortName.Text = model.ShortName;
            tb_Contacts.Text = model.Contacts;
            if (model.Tel != "")
            {
                tb_Tel_area.Text = model.Tel.Substring(0, 4);
                tb_Tel_real.Text = model.Tel.Substring(4);
            }
            tb_Mobile.Text = model.Mobile;
            tb_Address.Text = model.Address;
            tb_Description.Text = model.Description;

            tb_Province.Text = model.Province;
            tb_City.Text = model.City;
            tb_Area.Text = model.Area;

            tb_Pic.Text = modelPic.URL;

            cb_IsLock.Checked = modelUser.IsLock;
            rbl_BuyDay.SelectedValue = model.BuyDay.ToString();

            if (Security.GetCurrentUserType() == EnumHelper.UserType.Administrator)
            {
                group_StateCheck.Visible = modelUser.State == (int)EnumHelper.UserState.WaitCheck;
                group_IsLock.Visible = modelUser.State == (int)EnumHelper.UserState.Nomal;
            }
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            string Name = PageValidate.SqlTextClear(DataTypePlus.requestString(tb_Name.Text));
            string Password = PageValidate.SqlTextClear(tb_Password.Text);
            string City = PageValidate.SqlTextClear(tb_City.Text);
            string Mobile = PageValidate.SqlTextClear(tb_Mobile.Text);
            string Tel=PageValidate.SqlTextClear(tb_Tel_area.Text+tb_Tel_real.Text);

            if (
                Master.noErr(Name == "", "[孕婴店名称]必须填写") &&
                Master.noChongfu("Name='" + Name + "'", "[孕婴店名称]重复")&&
                Master.noChongfu("Mobile='" + Mobile + "'", "[手机号码]重复")&&
                Master.noChongfu("Tel<>'' and Tel='" + Tel + "'", "[办公电话]重复")
                )
            {
                //图片
                string Pic = DataTypePlus.requestString(tb_Pic.Text);
                if (model.PicID > 0 && modelPic.URL != Pic) bllPic.Delete(modelPic.id);//删除老图
                if (Pic == "") model.PicID = 0;
                else if (modelPic.URL != Pic)
                {
                    modelPic.URL = Pic;
                    System.Drawing.Image bmp = System.Drawing.Image.FromFile(this.Server.MapPath(Server.UrlDecode(Pic)));
                    modelPic.Width = bmp.Width;
                    modelPic.Height = bmp.Height;
                    model.PicID = bllPic.Add(modelPic);
                }
                //
                if (model.City != City)//变城市时变编号
                {
                    //Rank
                    List<Model.Store> listModelStore = bll.GetModelList("City='" + City + "' order by Rank desc");
                    if (listModelStore != null && listModelStore.Count > 0) model.Rank = listModelStore[0].Rank + 1;
                    else model.Rank = 1;
                    //Number
                    model.Number = "S" + tb_CityNo.Text.PadLeft(2, '0') + model.Rank.ToString().PadLeft(2, '0');//2
                }
                model.Name = Name;
                model.ShortName = PageValidate.SqlTextClear(tb_ShortName.Text);
                model.Contacts = PageValidate.SqlTextClear(tb_Contacts.Text);
                model.Tel = Tel;
                model.Mobile = PageValidate.SqlTextClear(tb_Mobile.Text);
                model.Address = PageValidate.SqlTextClear(tb_Address.Text);
                model.Description = tb_Description.Text;
                model.BuyDay = DataTypePlus.requestInt(rbl_BuyDay.SelectedValue);

                model.Province = PageValidate.SqlTextClear(tb_Province.Text);
                model.City = City;
                model.Area = PageValidate.SqlTextClear(tb_Area.Text);

                modelUser.Username = Name;
                modelUser.IsLock = cb_IsLock.Checked;
                modelUser.Mobile = Mobile;

                //
                if (Master.formState == EnumHelper.FormState.add)
                {
                    //添加User
                    if (Security.GetCurrentUserType() != EnumHelper.UserType.Administrator) modelUser.State = (int)EnumHelper.UserState.WaitCheck;
                    modelUser.Password = StringPlus.md5(Password);
                    modelUser.UserType = (int)EnumHelper.UserType.Store;
                    modelUser.State = (int)EnumHelper.UserState.Nomal;
                    modelUser.Mobile = model.Mobile;
                    int UserID_new = bllUser.Add(modelUser);
                    //添加Store
                    model.UserID = UserID_new;
                    Master.id = bll.Add(model);
                }
                else
                {
                    if (modelUser.State == (int)EnumHelper.UserState.WaitCheck)
                    {
                        if (cb_StateCheck.Checked) modelUser.State = (int)EnumHelper.UserState.Nomal;
                    }
                    else if (cb_IsLock.Checked) modelUser.State = (int)EnumHelper.UserState.Lock;
                    else modelUser.State = (int)EnumHelper.UserState.Nomal;
                    //
                    if (Password != "") modelUser.Password = StringPlus.md5(Password);
                    bllUser.Update(modelUser);
                    bll.Update(model);
                }
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Store, Master.id, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");

                Master.SubmitOK();

            }
        }
    }
}